﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Model;
using MsDAL;
using Model.Enum;

namespace UIDesign
{
    public partial class FrmSelectField : Form
    {
        CommonDAL dal = new CommonDAL();
        private List<Sys5a01> list = null;

        public FrmSelectField()
        {
            InitializeComponent();
        }

        public FrmSelectField(string code)
        {
            InitializeComponent();
            LoadTree();
        }

        private void LoadTree()
        {
            DataTable dt = dal.GetAllTable();
            TreeNode root = new TreeNode("表清单");
            root.Tag = "0";
            TreeNode node = null;
            foreach (DataRow dr in dt.Rows)
            {
                node = new TreeNode(dr["name"].ToString());
                node.Tag = "1";
                root.Nodes.Add(node);
            }

            this.tvLeft.Nodes.Add(root);
            this.tvLeft.ExpandAll();
        }

        private void tvLeft_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode node = e.Node;
            if ("0" == node.Tag.ToString())
            {
                return;
            }

            DataTable dt = dal.GetDetailByTableName(node.Text);
            this.dgvMain.DataSource = null;
            this.dgvMain.AutoGenerateColumns = true;
            this.dgvMain.DataSource = dt; //先清空数据源，后重新绑定
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Sys5a01 syLayout = null;

            foreach (DataGridViewRow dr in this.dgvMain.SelectedRows)
            {
                if (null == list)
                {
                    list = new List<Sys5a01>();
                }

                syLayout = new Sys5a01();
                syLayout.Orderid = 0;
                syLayout.Colname = dr.Cells["ColumnName"].Value.ToString();
                syLayout.Headcn = dr.Cells["Description"].Value.ToString();
                syLayout.Coltype = PsDataType.String;
                syLayout.Editmode = "A,M";
                syLayout.Controltype = ControlType.Text;
                syLayout.Defvalue = "";
                syLayout.Maxlength = Convert.ToInt32(dr.Cells["maxlength"].Value);
                syLayout.Digits = 0;
                syLayout.Notnull = !Convert.ToBoolean(dr.Cells["nullable"].Value);
                syLayout.Iskey = false;
                syLayout.Colspan = 1;
                syLayout.Isquery = true;
                syLayout.Columnparam = "";
                syLayout.Columnoption = "";
                syLayout.Remark = "自动生成";

                list.Add(syLayout);
            }
        }

        public List<Sys5a01> GetSelectedRow()
        {
            list.Reverse();
            return list;
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}
